package com.villaday.idf.custom.bannerUtil;

import android.content.Context;
import android.net.Uri;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.villaday.idf.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * RecyclerView的多(单)布局 适配器
 * 可以实现ListView、GridView和瀑布流的效果
 * <p>
 * 配置数据 -- 必须重写{@link #onBindHolder(RecyclerViewHolder, Object, int)}
 * 添加头部 -- {@link #addHeaderView(View)}
 * -- 添加头部后，部分item刷新(如{@link #notifyItemRemoved(int)})时 不可直接使用原始数据源的下标
 * 多布局实现 -- 必须重写{@link #getItemType(int)}方法
 * -- 强制要求数据源中有一个type名称的属性，重写时返回该值
 */
public abstract class AbsRecyclerViewAdapter<T> extends RecyclerView.Adapter<AbsRecyclerViewAdapter.RecyclerViewHolder> {

    protected Context context;
    protected List<T> data;
    private int[] resId;//多布局的布局文件

    public static final int TYPE_HEADER = -1;//第一条头部的类型
    private List<View> mHeaderViews = new ArrayList<>();

    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public AbsRecyclerViewAdapter(Context context, int... resId) {
        this.context = context;
        this.resId = resId;
        data = new ArrayList<>();
    }

    public AbsRecyclerViewAdapter(Context context, List<T> data, int... resId) {
        this.context = context;
        this.resId = resId;
        this.data = data;
    }

    public void setData(List<T> data) {
        this.data = data;
        this.notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        this.data.addAll(data);
        this.notifyDataSetChanged();
    }

    public List<T> getData() {
        return data;
    }

    public void deleteItem(int position) {
        this.data.remove(position);
        this.notifyDataSetChanged();
    }




    /**
     * Item点击监听 -- 添加头部后，头部的点击事件需自行处理
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    /**
     * Item长按监听 -- 添加头部后，头部的长按事件需自行处理
     */
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(View v, int position);
    }


    @Override
    public int getItemCount() {
        if (data == null) {
            return mHeaderViews.size();
        }
        return data.size() + mHeaderViews.size();
    }

    /**
     * 增加头部
     *
     * @param header
     */
    public void addHeaderView(View header) {
        if (header == null) {
            throw new RuntimeException("header is null");
        }
        mHeaderViews.add(header);
        this.notifyDataSetChanged();
    }

    /**
     * 清除头部
     */
    public void clearHeader() {
        if (mHeaderViews.size() > 0) {
            mHeaderViews.clear();
            this.notifyDataSetChanged();
        }
    }

    /**
     * 因支持添加头部功能 该方法不能重写 使用多布局时 只能重写 {@link #getItemType(int)}
     */
    @Override
    public int getItemViewType(int position) {
        int count = mHeaderViews.size();
        if (count > 0 && position < count) {//头部的类型
            return TYPE_HEADER - position;
        }
        return getItemType(position - count);
    }

    /**
     * 当前position所对应的Item的类型
     * 默认Item的类型为0，其他Item的类型依次递增
     */
    public int getItemType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType <= TYPE_HEADER) {
            return new RecyclerViewHolder(mHeaderViews.get(-1 - viewType));
        }
        View view = LayoutInflater.from(context).inflate(resId[viewType], parent, false);
        return new RecyclerViewHolder(view);
    }

    /**
     * 因支持添加头部功能 该方法不能重写 只能重写 {@link #onBindHolder(RecyclerViewHolder, Object, int)}
     */
    @Override
    public void onBindViewHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, int position) {
        if (getItemViewType(position) <= TYPE_HEADER) {
            return;
        }
        onBindHolder(holder, data.get(position - mHeaderViews.size()), position - mHeaderViews.size());
    }

    /**
     * 配置数据 -- holder的类型必须是 AbsRecyclerViewAdapter.RecyclerViewHolder
     * 整条Item避免setTag()，如必须，需设置id
     */
    public abstract void onBindHolder(RecyclerViewHolder holder, T d, int position);

    /**
     * GridLayoutManager时 设置type不为0的item 占整行位置
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == 0 ? 1 : gridManager.getSpanCount();
                }
            });
        }
    }

    /**
     * StaggeredGridLayoutManager时 设置type不为0的item 占整行位置
     */
    @Override
    public void onViewAttachedToWindow(AbsRecyclerViewAdapter.RecyclerViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null) {
            if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) lp;
                layoutParams.setFullSpan(holder.getItemViewType() != 0);
            } else if (!(lp instanceof GridLayoutManager.LayoutParams)) {
                /**
                 * LinearLayoutManager时 头部item撑满
                 */
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) lp;
                layoutParams.width = RecyclerView.LayoutParams.MATCH_PARENT;
                layoutParams.height = RecyclerView.LayoutParams.WRAP_CONTENT;
                holder.itemView.setLayoutParams(layoutParams);
            }
        }
    }

    protected class RecyclerViewHolder extends RecyclerView.ViewHolder {

        Map<Integer, View> cacheMap = new HashMap();
        View layoutView;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            if (onItemClickListener != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = RecyclerViewHolder.this.getLayoutPosition();
                        if (position >= mHeaderViews.size()) {
                            onItemClickListener.onItemClick(v, position - mHeaderViews.size());
                        }
                    }
                });
            }
            if (onItemLongClickListener != null) {
                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = RecyclerViewHolder.this.getLayoutPosition();
                        if (position >= mHeaderViews.size()) {
                            return onItemLongClickListener.onItemLongClick(v, position - mHeaderViews.size());
                        }
                        return true;
                    }
                });
            }
            this.layoutView = itemView;
        }


        public View getView(int id) {
            if (cacheMap.containsKey(id)) {
                return cacheMap.get(id);
            } else {
                View view = layoutView.findViewById(id);
                cacheMap.put(id, view);
                return view;
            }
        }


        /**
         * 绑定TextView
         */
        public RecyclerViewHolder bindTextView(int id, String text) {
            TextView tv = (TextView) getView(id);
            tv.setText(text);
            return this;
        }

        /**
         * 绑定SpannableString
         */
        public RecyclerViewHolder bindSpannableString(Context context, int id, String text, String key) {
            TextView tv = (TextView) getView(id);
            int first = text.indexOf(key);
            int last = first + key.length();
            SpannableString spannableString = new SpannableString(text);
            spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimary)), first, last, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(spannableString);
            return this;
        }

        /**
         * 绑定ImageView
         */
        public RecyclerViewHolder bindImageView(int id, int imgId) {
            ImageView iv = (ImageView) getView(id);
            iv.setImageResource(imgId);
            return this;
        }


        /**
         * 绑定Glide
         */
        public RecyclerViewHolder bindGlide(Context context, int id, Uri uri) {
            ImageView imageView = (ImageView) getView(id);
            Glide.with(context)
                    .load(uri)

                    .into(imageView);
            return this;
        }




        /**
         * 设置进度条进度
         */
        public RecyclerViewHolder bindProgressBar(int id, int num) {
            ProgressBar progressBar = (ProgressBar) getView(id);
            progressBar.setProgress(num);
            return this;
        }

    }
}
